<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Youzi
 * Date: 12-12-4
 * Time: 下午10:35
 * To change this template use File | Settings | File Templates.
 */
class OaModel extends model
{
    static $_instance = null;

	static function getInstance ()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    function getDepartmentAll($staff)
    {
        $condition = '';
        if ($staff['manage_agency_id'] && $staff['agency_id'] != 1) {
            $condition = ' and id in ('.$condition.')';
        }
//        return $this->sql('select * from crm_agency where parent>0 and stop=0'.$condition)->s();
        return $this->sql('select * from crm_agency where stop=0'.$condition)->s();
    }

	function getDepartmentListAll($page=1, $pageSize=10){
        $limit = ($page-1)*$pageSize.",".$pageSize;
        return $this->sql('select * from crm_department where parent>0 and stop=0 limit '.$limit)->s();
    }

	function getDepartmentListCount()
    {
        $row = $this->sql('select count(*) as total from crm_department where parent>0 and stop=0')->s(1);
        return $row['total'];
    }

	function DepartmentEdit($prarm){
        return $this->sql("update crm_department set `name`='[name]',`contact`='[contact]',`desc`='[desc]' where id=[id]",$prarm)->d();
    }

	function DepartmentAdd($prarm){
        return $this->sql("insert into crm_department (`name`,`contact`,`desc`)values('[name]','[contact]','[desc]')",$prarm)->i();
    }

    function StaffByContact($p){
        $bus = $this->sql("select group_concat(contact) as contact from crm_agency where FIND_IN_SET('[cid]',contact)",$p)->s(1);
        return $bus["contact"];
    }

    public function getStaffInfo($uid)
    {
        $sql = "select a.id as staff_id,a.*,b.*
                from crm_staff as a
                left join sys_user as b on a.login_id=b.id
                where a.login_id=$uid and a.stop=0 limit 1";
        $row = $this->sql($sql)->s(1);
        if (!$row) {
            return false;
        }
        $row['manage_agency'] = $this->is_agency_contact($row['staff_id']);
        $row['manage_agency_id'] = '';
        if ($row['manage_agency']) {
            $tmp = array();
            foreach ($row['manage_agency'] as $item) {
                $tmp[] = $item['id'];
            }
            $row['manage_agency_id'] = implode(',', $tmp);
        }

        return $row;
    }

    public function is_agency_contact($staff_id)
    {
        $sql = "select * from crm_agency where FIND_IN_SET($staff_id, contact) and stop=0";
        return $this->sql($sql)->s();
    }

    public function getStaffAll($staff)
    {
        $condition = '';
        if ($staff['manage_agency_id'] && $staff['agency_id'] != 1) {
            $condition = ' b.agency_id in ('.$staff['manage_agency_id'].') and ';
        }
        $sql = "select a.id as staff_id,a.*,b.*
                from crm_staff as a
                left join sys_user as b on a.login_id=b.id
                where $condition a.stop=0 and b.agency_id>0 and b.ugroup=2";
        return $this->sql($sql)->s();
    }

    public function getStaffRow($condition, $field='*')
    {
        $sql = "select $field from crm_staff where $condition limit 1";
        return $this->sql($sql)->s(1);
    }

    function AgencyListAll(){
        return $this->sql('select id,parent,name as text from crm_agency where stop=0 order by parent ')->s();
    }

    /**
     * 会议记录列表
     *
     * @param  string $sorts
     * @param  int  $page
     * @param  int  $pagesize
     * @return array
     */
    public function getStaffList($sorts = 'id', $page = 1, $pagesize = 10)
    {
        $sql = 'select a.*,t.user as login_name,c.name as agency_name
             from crm_staff as a
             left join sys_user as t on a.login_id=t.id
             left join crm_agency as c on t.agency_id=c.id
             where a.stop=0 and t.ugroup=2 order by a.'.$sorts.' asc ';
        $limit = " limit ".($page-1)*$pagesize.",".$pagesize;
        $list = $this->sql($sql.$limit)->s();
        return $list;
    }

    public function getStaffCount()
    {
        $sql = "";
        $list = $this->sql('select count(*) as c from crm_staff where stop=0 ')->s(1);
        return $list['c'];
    }

    public function getNotepadList($uid, $page = 1, $pagesize = 10)
    {
        $sql = "select a.*,a.content as coontents
                from crm_notepad as a
                left join crm_staff as b on a.staff_id=b.id
                left join sys_user as c on b.login_id=c.id
                where c.id=$uid and a.`stop`=0 and b.`stop`=0 and c.`stop`=0
                order by a.id desc";
        $limit = " limit ".($page-1)*$pagesize.",".$pagesize;

        return $this->sql($sql.$limit)->s();
    }

    public function getNotepadCount($uid)
    {
        $sql = "select count(*) as n
                from crm_notepad as a
                left join crm_staff as b on a.staff_id=b.id
                left join sys_user as c on b.login_id=c.id
                where c.id=$uid and a.`stop`=0 and b.`stop`=0 and c.`stop`=0
                order by a.created desc";
        $row = $this->sql($sql)->s(1);
        return $row['n'];
    }

    public function AddNotepad($data)
    {
        $sql = insertSql('crm_notepad', $data);
        return $this->sql($sql)->i();
    }

    public function EditNotepad($data, $id)
    {
        $sql = updateSql('crm_notepad', $data, 'id='.$id);
        return $this->sql($sql)->u();
    }

    public function DelNotepad($id)
    {
        $sql = "update crm_notepad set stop=1 where id in ($id)";
        return $this->sql($sql)->u();
    }

    public function staffEdit($data, $id)
    {
        $sql = updateSql("crm_staff", $data, "id=".$id);
        return $this->sql($sql)->u();
    }

    public function userEdit($data, $id)
    {
        $sql = updateSql("sys_user", $data, "id=".$id);
        return $this->sql($sql)->u();
    }

    public function getNotepadInfo($nid)
    {
        $sql = "select * from crm_notepad where id=$nid and stop=0";
        return $this->sql($sql)->s(1);
    }


    public function getScheduleList($uid, $page = 1, $pagesize = 10)
    {
        $sql = "select a.*,a.content as contents
                from crm_schedule as a
                left join crm_staff as b on a.staff_id=b.id
                left join sys_user as c on b.login_id=c.id
                where c.id=$uid and a.`stop`=0 and b.`stop`=0 and c.`stop`=0
                order by a.id desc";
        $limit = " limit ".($page-1)*$pagesize.",".$pagesize;

        return $this->sql($sql.$limit)->s();
    }

    public function getScheduleCount($uid)
    {
        $sql = "select count(*) as n
                from crm_schedule as a
                left join crm_staff as b on a.staff_id=b.id
                left join sys_user as c on b.login_id=c.id
                where c.id=$uid and a.`stop`=0 and b.`stop`=0 and c.`stop`=0
                order by a.created desc";
        $row = $this->sql($sql)->s(1);
        return $row['n'];
    }

    public function AddSchedule($data)
    {
        $sql = insertSql('crm_schedule', $data);
        return $this->sql($sql)->i();
    }

    public function EditSchedule($data, $id)
    {
        $sql = updateSql('crm_schedule', $data, 'id='.$id);
        return $this->sql($sql)->u();
    }

    public function DelSchedule($id)
    {
        $sql = "update crm_schedule set stop=1 where id in ($id)";
        return $this->sql($sql)->u();
    }

    public function getScheduleInfo($nid)
    {
        $sql = "select * from crm_schedule where id=$nid and stop=0";
        return $this->sql($sql)->s(1);
    }

    public function getWeeklyReportsList($condition, $page = 1, $pagesize = 10)
    {
        $sql = "select a.*,a.content as contents,b.name as staff_name,d.name as agency_name, e.name as position_name
                from crm_weeklyReports as a
                left join crm_staff as b on a.staff_id=b.id
                left join sys_user as c on b.login_id=c.id
                left join crm_agency as d on c.agency_id=d.id
                left join crm_position as e on c.position_id=e.id
                where $condition and a.`stop`=0 and b.`stop`=0 and c.`stop`=0
                order by a.id desc";//echo $sql;exit;
        $limit = " limit ".($page-1)*$pagesize.",".$pagesize;

        return $this->sql($sql.$limit)->s();
    }

    public function getWeeklyReportsCount($condition)
    {
        $sql = "select count(*) as n
                from crm_weeklyReports as a
                left join crm_staff as b on a.staff_id=b.id
                left join sys_user as c on b.login_id=c.id
                where $condition and a.`stop`=0 and b.`stop`=0 and c.`stop`=0
                order by a.created desc";
        $row = $this->sql($sql)->s(1);
        return $row['n'];
    }

    public function AddWeeklyReports($data)
    {
        $sql = insertSql('crm_weeklyReports', $data);
        return $this->sql($sql)->i();
    }

    public function EditWeeklyReports($data, $id)
    {
        $sql = updateSql('crm_weeklyReports', $data, 'id='.$id);
        return $this->sql($sql)->u();
    }

    public function DelWeeklyReports($id)
    {
        $sql = "update crm_weeklyReports set stop=1 where id in ($id)";
        return $this->sql($sql)->u();
    }

    public function getWeeklyReportsInfo($nid)
    {
        $sql = "select * from crm_weeklyReports where id=$nid and stop=0";
        return $this->sql($sql)->s(1);
    }

    public function getReceivablesList($condition, $page = 1, $pagesize = 10)
    {
        $sql = "select a.*,b.name as customer_name, c.name as staff_name
                from crm_receivables as a
                left join crm_customer as b on a.customer_id=b.id
                left join crm_staff as c on a.staff_id=c.id
                where $condition and a.`stop`=0 and b.`stop`=0 and c.`stop`=0
                order by a.id desc";//echo $sql;exit;
        $limit = " limit ".($page-1)*$pagesize.",".$pagesize;

        return $this->sql($sql.$limit)->s();
    }

    public function getReceivablesCount($condition)
    {
        $sql = "select count(*) as n
                from crm_receivables as a
                left join crm_customer as b on a.customer_id=b.id
                left join crm_staff as c on a.staff_id=c.id
                where $condition and a.`stop`=0 and b.`stop`=0 and c.`stop`=0";
        $row = $this->sql($sql)->s(1);
        return $row['n'];
    }

    public function AddReceivables($data)
    {
        $sql = insertSql('crm_receivables', $data);
        return $this->sql($sql)->i();
    }

    public function EditReceivables($data, $id)
    {
        $sql = updateSql('crm_receivables', $data, 'id='.$id);
        return $this->sql($sql)->u();
    }

    public function DelReceivables($id)
    {
        $sql = "update crm_receivables set stop=1 where id in ($id)";
        return $this->sql($sql)->u();
    }

    public function getReceivablesInfo($nid)
    {
        $sql = "select * from crm_receivables where id=$nid and stop=0";
        return $this->sql($sql)->s(1);
    }

    public function getAreaList()
    {
        $sql = "select id,parent,name as text from crm_area where stop=0";
        return $this->sql($sql)->s();
    }

    public function getAftermarketList($condition, $page = 1, $pagesize = 10)
    {
        $sql = "select a.*,b.name as customer_name, c.name as staff_name
                from crm_aftermarket as a
                left join crm_customer as b on a.customer_id=b.id
                left join crm_staff as c on a.staff_id=c.id
                where $condition and a.`stop`=0 and b.`stop`=0 and c.`stop`=0
                order by a.id desc";//echo $sql;exit;
        $limit = " limit ".($page-1)*$pagesize.",".$pagesize;

        return $this->sql($sql.$limit)->s();
    }

    public function getAftermarketCount($condition)
    {
        $sql = "select count(*) as n
                from crm_aftermarket as a
                left join crm_customer as b on a.customer_id=b.id
                left join crm_staff as c on a.staff_id=c.id
                where $condition and a.`stop`=0 and b.`stop`=0 and c.`stop`=0";
        $row = $this->sql($sql)->s(1);
        return $row['n'];
    }

    public function AddAftermarket($data)
    {
        $sql = insertSql('crm_aftermarket', $data);
        return $this->sql($sql)->i();
    }

    public function EditAftermarket($data, $id)
    {
        $sql = updateSql('crm_aftermarket', $data, 'id='.$id);
        return $this->sql($sql)->u();
    }

    public function DelAftermarket($id)
    {
        $sql = "update crm_aftermarket set stop=1 where id in ($id)";
        return $this->sql($sql)->u();
    }

    public function getMyMsg($cid)
    {
        $sql = "select *
                from crm_business_log
                where receive_flg=0 and receive=$cid
                order by accept_flg asc, created desc
                limit 20";
        return $this->sql($sql)->s();
    }

    public function getMsgView($cid, $bid)
    {
        $sql = "select *
                from crm_business_log
                where receive_flg=0 and receive=$cid and id=$bid
                limit 1";
        return $this->sql($sql)->s(1);
    }

    public function editMsg($data, $id)
    {
        $sql = updateSql('crm_business_log', $data, 'id='.$id);
        return $this->sql($sql)->u();
    }
}
